<!-- 库内管理/待返架 -->
<template>
  <div style="padding: 15px">
    <vxe-toolbar
      id="warehouseLocationId"
      :custom="{ storage: true }"
      export
      :refresh="{ query: findList }"
    >
      <template v-slot:buttons>
        <el-button @click="listQuery.pageNum = 1; findList()">查询</el-button>
        <el-button :disabled="btnDisabled" @click="backRack">返架</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      :export-config='{type:"csv"}'
      stripe
      highlight-current-row
      align="center"
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :data="tableData"
      :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }"
      sort-config.remote
      @sort-change="sortList"
      auto-resize
      @checkbox-all="checkboxChangeEvent"
      @checkbox-change="checkboxChangeEvent"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60" fixed="left" />
      <vxe-table-column type="checkbox" width="60" fixed="left" />
      <vxe-table-column>
        <vxe-table-column
          field="warehouseCode"
          title="仓库"
          min-width="150"
          sortable
          :formatter="formatterWarehouse"
        />
        <template #header>
          <el-select
            v-model="listQuery.warehouseCode"
            placeholder="请选择仓库 "
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in warehouseData"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="locationName"
          title="库位"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.locationName"
            placeholder="请输入库位"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="oldLocationName"
          title="原库位"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.oldLocationName"
            placeholder="请输入原库位"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsName"
          title="商品名称"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.goodsName"
            placeholder="请输入商品名称"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodCode"
          title="商品编码"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.goodCode"
            placeholder="请输入商品编码"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsBarCode"
          title="商品条码"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.goodsBarCode"
            placeholder="请输入商品条码"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsUnitName"
          title="基本单位"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.unitName"
            placeholder="请输入基本单位"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="quantity"
          title="库存数量"
          min-width="180"
          sortable
        />
        <template #header>
          <el-row type="flex" align="middle" style="color: #dcdfe6">
            <el-input
              v-model="listQuery.quantityFloor"
              placeholder="下限"
              type='number'
              clearable
              ref="quantity"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.quantityCeiling"
              placeholder="上限"
              type='number'
              clearable
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="containerBarCode"
          title="容器号"
          min-width="200"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-input
            v-model="listQuery.containerBarCode"
            placeholder="请输入容器号"
            clearable
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="expirationQuantity"
          title="保质期天数"
          min-width="180"
          sortable
        />
        <template #header>
          <el-row type="flex" align="middle" style="color: #dcdfe6">
            <el-input
              v-model="listQuery.expirationQuantityFloor"
              placeholder="下限"
              type='number'
              clearable
              ref="quantity"
            />
            &nbsp;-&nbsp;
            <el-input
              v-model="listQuery.expirationQuantityCeiling"
              placeholder="上限"
              type='number'
              clearable
            />
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtManufacture"
          title="生产日期"
          min-width="400"
          sortable
        />
        <template #header>
          <el-date-picker
            v-model="gmtManufacture"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtManufacture')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtExpire"
          title="过期日期"
          min-width="400"
          sortable
        />
        <template #header>
          <el-date-picker
            v-model="gmtExpire"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtExpire')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtStock"
          title="存货日期"
          min-width="400"
          sortable
        />
        <template #header>
          <el-date-picker
            v-model="gmtStock"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="(value) => changeDate(value, 'gmtStock')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsStatusName"
          title="库存状态"
          min-width="140"
          sortable
          show-header-overflow
          show-overflow
        />
        <template #header>
          <el-select
            v-model="listQuery.goodsStatusName"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in goodsStatusList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="lockName"
          title="锁定状态"
          min-width="140"
          sortable
          show-header-overflow
          show-overflow
          :formatter="({ cellValue }) => this.getEnumsOrDict('dict', 'lockStatusList').get(String(cellValue))"
        />
        <template #header>
          <el-select
            v-model="listQuery.isLock"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in getEnumsOrDict('enum', 'lockStatusList')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="[
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'Sizes',
        'FullJump',
        'Total',
      ]"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import { backRackList, backRack, chooseDataByAuthShipment } from "@/api/store";
import { getWarehouse } from "@/api/basicInfo";

export default {
  name: 'BackRack',
  data() {
    return {
      loading: false,
      btnDisabled: true, // 按钮是否禁用
      selectData: [], // 选中的数据
      // 日期选项
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      statusList: [
        //状态
        { code: "0", name: "新增" },
        { code: "1", name: "待审核" },
        { code: "2", name: "审核通过" },
        { code: "3", name: "驳回" },
        { code: "4", name: "作废" },
      ],
      warehouseData: [],
      goodsStatusList: [],
      tableData: [],
      gmtManufacture: "",
      gmtExpire: "",
      gmtStock: "",
      listQuery: {
        total: 0, //总数
        pageNum: 1,
        pageSize: 20,
      },
    };
  },
  created() {
    this.getWarehouse()
  },
  methods: {
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 翻译仓库
    formatterWarehouse({ cellValue }) {
      const sd = this.warehouseData.find((i) => i.code === cellValue);
      return sd ? sd.name : "";
    },

    // 仓库查询
    getWarehouse() {
      chooseDataByAuthShipment().then((res) => {
        this.goodsStatusList = res.data
      })
      return getWarehouse().then((res) => {
        this.warehouseData = res.data
      }).finally(()=>this.findList())
    },

    //获取列表
    findList() {
      this.loading = true
      return backRackList(this.listQuery).then((res) => {
        this.tableData = res.data.records
        this.listQuery.total = res.data.total
      }).finally(()=> this.loading = false)
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ""
      }
      this.findList()
    },
    backRack() {
      this.$confirm('确认返架吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          const rLoading = this.openLoading({
            text: '返架中...',
            duration: 'x'
          })
          const inventoryCodes = this.selectData.map(item => item.code)
          backRack(inventoryCodes).then((res) => {
            this.$message({ dangerouslyUseHTMLString: true, message: res.msg, type: 'success' })
          }).finally(()=>{
            this.findList()
            this.$refs.xTable.clearCheckboxRow()
            this.btnDisabled = !this.btnDisabled
            rLoading.close()
          })
        })
        .catch(() => {})
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    // 全选与否、部分勾选与否
    checkboxChangeEvent({ selection }) {
      this.selectData = selection
      this.btnDisabled = selection.length < 1
    },
  }
}
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}
.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
